package org.liubility.server.commons.handler;

import com.jdragon.common.response.normal.Result;
import org.liubility.pojo.exception.LBException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: Jdragon
 * @email: 1061917196@qq.com
 * @Date: 2020.09.04 16:42
 * @Description:
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    @ResponseBody
    public Result<String> getMessage(MethodArgumentNotValidException exception) {
        // 获取NotNull注解中的message
        String message = exception.getBindingResult().getFieldError().getDefaultMessage();
        return Result.paramsError(message);
    }

    @ExceptionHandler(value = {UnknownError.class})
    @ResponseBody
    public Result<String> getMessage(UnknownError exception) {
        return Result.unKnowError(exception.getMessage());
    }

    @ExceptionHandler(value = {LBException.class})
    @ResponseBody
    public Result<String> getMessage(LBException exception) {

        return Result.error(exception.getMessage());
    }
}
